package com.mall.manager.service;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;

import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import com.mall.common.dao.MongoDBManager;
import com.mall.common.dao.MysqlDBManager;
import com.mall.untils.DateUtils;
import com.mongodb.DBObject;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
/**
 * @全国园所首次登录统计
 * @author liugb
 * @Date 2016 7 13
 */
@Service
public class FirstGardenStatisticsService {
	@Resource
    private JdbcTemplate jdbcTemplate;
	//日志信息
	private Logger logger = LoggerFactory.getLogger(FirstGardenStatisticsService.class);	
	/**
	 * 全国园所首次登录统计信息
	 * @return
	 */
	public List<String> findFirstGardenStatistics() {
		List<DBObject> findAll = MongoDBManager.findAll("html_collection");
		List<DBObject> school = MongoDBManager.findAll("php_school");
		List<String> stingList = new ArrayList<String>();
		if(findAll.size()>0&&school.size()>0){
			Map<String,DBObject> mapSchool = new HashMap<String,DBObject>();
			for(DBObject sch:school){
				mapSchool.put("school_"+sch.get("id"),sch);
			}
//			Map<String,DBObject> htmlSchool = new HashMap<String,DBObject>();	
			try {
				for(DBObject dbo:findAll){
					int schoolId = 0;
					if(dbo.get("kinder_id")!=null){
					   schoolId =  (Integer) dbo.get("kinder_id");					
					}
					DBObject sch = mapSchool.get("school_"+schoolId);
					if(sch != null){					
						dbo.put("province", sch.get("province"));
						dbo.put("city", sch.get("city"));
						dbo.put("area", sch.get("area"));
						dbo.put("address", sch.get("address"));
						dbo.put("starttime", dbo.get("start_time"));
						dbo.put("endtime", dbo.get("end_time"));
						dbo.put("datatype", dbo.get("data_type"));
						stingList.add(dbo.toString());
					}									
				}
			} catch (Exception e) {
				// TODO: handle exception
				logger.debug(e.getMessage());
			}					
		}
		return stingList;			
	}	
	/**
	 * 保存数据
	 * @param map
	 * @param sum
	 * @throws SQLException 
	 */
	public static void manager_insert_firstgarden(String name,String rex,Integer score,Map<String,Object> map) throws SQLException{
		Connection conn = null;
		PreparedStatement ps = null;
		try {	
			String areaname = name.substring(0,name.length()-2);
			String code = null;
			code = MapUtils.getString(map, areaname);		
			String sql = "select * from t_ability_statistics where area_id = '"+code+"'";
			conn = MysqlDBManager.getConnection();
			ps = (PreparedStatement) conn.prepareStatement(sql);
			ResultSet res = ps.executeQuery();
			if(res.next()){
				sql="insert into t_first_garden_statistics(g_number,area_id,area_name,all_time,create_time,update_time) values(?,?,?,?,?,?)";
				ps = (PreparedStatement) conn.prepareStatement(sql);
				ps.setInt(1, 0);
				ps.setString(2, code);
				ps.setString(3, name);
				ps.setInt(4, score);
				ps.setTimestamp(5, DateUtils.getNowBeforTimestamp());
				ps.setTimestamp(6, DateUtils.getTimestamp());
				ps.addBatch();
				ps.executeBatch();
				conn.commit();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			conn.close();
			ps.close();
		}
	}
	
}
